/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.convert;

import java.util.TimeZone;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.FacesConverter;

@FacesConverter("ru.ssau.managed.SetDateConverter")
public class SetDateConverter extends DateTimeConverter implements Converter {

    public SetDateConverter() {
        super();
        setPattern("dd-MM-yyyy HH:mm:ss");
        setTimeZone(TimeZone.getDefault());
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String newValue) throws ConverterException {
        HtmlSelectBooleanCheckbox setCurrentDateCheckbox =
                (HtmlSelectBooleanCheckbox) component.findComponent("setCurrentDate");
        Boolean setCurrentDate = (Boolean) setCurrentDateCheckbox.getValue();
        if(setCurrentDate) {return null;}
        else if(newValue.trim().isEmpty()) {throw new ConverterException("Date is not set");}
        return super.getAsObject(context, component, newValue);
    }
}
